import VueRouter from "vue-router";

import Vue from "vue";
Vue.use(VueRouter)

// 留言板
import LeaveManage from "@/components/LeaveManage.vue";

import LogOn from "@/components/LogOn.vue";
import BackManage from "@/view/BackManage.vue";
import usermanage from "@/view/UserManage.vue";
import PartManage from "@/view/PartManage.vue";
import RepaireManage from "@/view/RepaireManage.vue";
import InventManage from "@/view/InventManage.vue";
import BackendHome from "@/view/BackendHome.vue";
import RolesManage from "@/view/RolesManage.vue";
import PermissionManage from "@/view/PermissionManage.vue";
import MenuManage from "@/view/MenuManage.vue";
import EditInfo from "@/view/EditInfo.vue";
import MaintSearch from "@/view/MaintSearch.vue";
import MaintEfficiency from "@/view/MaintEfficiency.vue";
import InventSearch from "@/view/InventSearch.vue";
import GoodsCategory from "@/view/GoodsCategory.vue";
let routes = [
    {
        path: "/",
        redirect: "/login"
    },
    {
        path: "/login",
        name: "Login",
        component: LogOn
    },
    {
        path: "/LeaveManage",
        name: "LeaveManage",
        component: LeaveManage
    },
    {
        path: "/backmanage",
        name: "backmanage",
        component: BackManage,
        children: [
            {
                path: "usermanage",
                name: "usermanage",
                component: usermanage,
                meta:{
                    title:"用户管理",
                    sub_title:"人员列表"
                }
            },
            {
                path: "GoodsCategory",
                name: "GoodsCategory",
                component: GoodsCategory,
                meta:{
                    title:"库存管理",
                    sub_title:"商品列表"
                }
            },
            {
                path: "PartManage",
                name: "PartManage",
                component: PartManage,
                meta:{
                    title:"部门管理",
                    sub_title:"部门列表"
                }
            },
            {
                path: "RepaireManage",
                name: "RepaireManage",
                component: RepaireManage,
                meta:{
                    title:"报修管理",
                    sub_title:"报修列表"
                }
            },
            {
                path: "InventManage",
                name: "InventManage",
                component: InventManage,
                meta:{
                    title:"库存管理",
                    sub_title:"库存列表"
                }
            },
            {
                path: "BackendHome",
                name: "BackendHome",
                component: BackendHome,
                meta:{
                    title:"工作台",
                    sub_title:"数据总览"
                }
            },
            {
                path: "RolesManage",
                name: "RolesManage",
                component: RolesManage,
                meta:{
                    title:"角色管理",
                    sub_title:"角色列表"
                }
            },
            {
                path: "PermissionManage",
                name: "PermissionManage",
                component: PermissionManage,
                meta:{
                    title:"权限管理",
                    sub_title:"权限列表"
                }
            },
            {
                path: "MenuManage",
                name: "MenuManage",
                component: MenuManage,
                meta:{
                    title:"菜单管理",
                    sub_title:"菜单列表"
                }
            },
            {
                path: "EditInfo",
                name: "EditInfo",
                component: EditInfo,
                meta:{
                    title:"个人中心",
                    sub_title:"修改信息"
                }
            },
            {
                path: "MaintSearch",
                name: "MaintSearch",
                component: MaintSearch,
                meta:{
                    title:"统计分析",
                    sub_title:"维修分析"
                }
            },
            {
                path: "MaintEfficiency",
                name: "MaintEfficiency",
                component: MaintEfficiency,
                meta:{
                    title:"统计分析",
                    sub_title:"维修效率"
                }
            },
            {
                path: "InventSearch",
                name: "InventSearch",
                component: InventSearch,
                meta:{
                    title:"统计分析",
                    sub_title:"库存分析"
                }
            },
        ]
    }
]

let router = new VueRouter({
    routes,
})

//解决 vue-router在3.0版本以上重复点击报错问题
const originalPush=VueRouter.prototype.push;
VueRouter.prototype.push = function push(location){
    return originalPush.call(this,location).catch(err=>err)
}

let isAuthenticated = (()=>{
    const token = localStorage.getItem("token") && localStorage.getItem("token_is_exp") !=1 ;
    if(token){
       return true 
    }
    return false
})
router.beforeEach((to, from, next) => {
    if (to.name !== 'Login' && !isAuthenticated()) next({ name: 'Login' })
    else next()
})
export default router